﻿namespace ClientRouting
{
    using System.Linq;
    using System.Collections.Generic;

    using Transport;

    public class RouterImpl<T> : Router<T> where T : Service
    {
        static IList<Connector<T>> _currentConnectors;

        public T FirstAvailable
        {
            get
            {
                return _currentConnectors[0].Proxy;
            }
        }

        public RouterImpl()
        {
            _currentConnectors = new List<Connector<T>>();
        }

        public void Register(Connector<T> connector)
        {
            if (_currentConnectors.Any(x => x.Equal(connector)))
            {
                throw new ClientRoutingException("connection is already present in the list of available connections");
            }

            if (connector.Proxy.IsOnline() == false)
            {
                throw new ClientRoutingException("connection dont seem online");
            }
            else
            {
                _currentConnectors.Add(connector);
                return;
            }
        }
    }
}
